package com.mall.biz.discount;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ibm.icu.math.BigDecimal;
import com.mall.client.item.MallItemService;
import com.mall.client.mall.MallMarketingService;
import com.mall.dto.DiscountDTO;
import com.mall.dto.DiscountMessageDTO;
import com.mall.pojo.MallItem;
import com.mall.pojo.MallMarketing;
import com.mall.query.DiscountItemQuery;
import com.mall.query.DiscountQuery;
import com.mall.query.MallMarketingQuery;

/**
 * 优惠的biz
 * 
 * @author Administrator
 *
 */
@Service
public class DiscountBizImpl {

	@Autowired
	private MallMarketingService mallMarketingService;
	@Autowired
	private MallItemService mallItemService;

	/**
	 * 获取折扣信息
	 * 
	 * @param discountQuery
	 * @return
	 */
	public List<DiscountDTO> getDiscountDTOListByDiscountQuery(DiscountQuery discountQuery) {
		List<DiscountDTO> discountDTOList = new ArrayList<>();
		List<DiscountItemQuery> discountItemQueryList = null;
		List<Long> itemIdList = null;
		if (discountQuery == null) {
			return null;
		}
		MallMarketingQuery mallMarketingQuery = new MallMarketingQuery();
		if (discountQuery.getBuyUserId() != null && discountQuery.getBuyUserId() > 0) {

		}
		if (discountQuery.getMallId() != null && discountQuery.getMallId() > 0) {
			mallMarketingQuery.setMallId(discountQuery.getMallId());
		}
		if (discountQuery.getCommOnlyKey() != null && discountQuery.getCommOnlyKey() > 0) {
			mallMarketingQuery.setOnlyKey(discountQuery.getCommOnlyKey());
		}
		if (discountQuery.getDiscountItemQueryList() != null && discountQuery.getDiscountItemQueryList().size() > 0) {
			discountItemQueryList = discountQuery.getDiscountItemQueryList();
			itemIdList = new ArrayList<>();
			for (DiscountItemQuery discountItemQuery : discountItemQueryList) {
				itemIdList.add(discountItemQuery.getBuyItemId());
			}
			mallMarketingQuery.setMallItemIds(itemIdList);
		}

		if (discountItemQueryList == null || itemIdList == null) {
			return null;
		}

		// 获取商品的所有优惠
		List<MallMarketing> mallMarketingList = mallMarketingService
				.getMallMarketingListByMallMarketingQuery(mallMarketingQuery, null);

		// 获取所有宝贝
		List<MallItem> mallItemList = mallItemService.getMallItemByIds(itemIdList);

		DiscountDTO discountDTO = null;
		MallItem mallItem = null;
		for (int i = 0; i < discountItemQueryList.size(); i++) {
			discountDTO = new DiscountDTO();
			mallItem = null;
			for (int j = 0; j < mallItemList.size(); j++) {
				if (discountItemQueryList.get(i).getBuyItemId().longValue() == mallItemList.get(i).getId()
						.longValue()) {
					mallItem = mallItemList.get(i);
					// discountDTO.setMallItem(mallItem);
				}
			}

			discountDTO.setBuyItemCount(discountItemQueryList.get(i).buyItemCount);
			discountDTO.setId(discountItemQueryList.get(i).getBuyItemId());
			setDiscountPrice(mallItem, discountDTO, mallMarketingList);
			discountDTOList.add(discountDTO);// 设置初始值
		}

		return discountDTOList;
	}

	/**
	 * 设置折扣信息
	 * 
	 * @param mallItem
	 * @param discountDTO
	 * @param mallMarketingList
	 */
	private void setDiscountPrice(MallItem mallItem, DiscountDTO discountDTO, List<MallMarketing> mallMarketingList) {

		List<DiscountMessageDTO> discountMessageDTOList = new ArrayList<>();
		DiscountMessageDTO discountMessageDTO = null;

		List<MallMarketing> itemMallMarketingList = new ArrayList<>();

		if (mallMarketingList != null && mallMarketingList.size() > 0) {/* 获取这个商品的优惠信息 */
			for (MallMarketing mallMarketing : mallMarketingList) {
				if (mallMarketing.getItemId() != null
						&& mallMarketing.getItemId().longValue() == mallItem.getId().longValue()) {
					itemMallMarketingList.add(mallMarketing);
				}
			}
		}

		if (mallMarketingList == null || mallMarketingList.size() <= 0 || itemMallMarketingList == null
				|| itemMallMarketingList.size() <= 0) {/* 如果没有优惠信息，则返回原价 */
			discountMessageDTO = new DiscountMessageDTO();
			discountMessageDTO.setBuyItemCount(discountDTO.getBuyItemCount());
			discountMessageDTO.setDiscountPrice(mallItem.getPrice());
			discountMessageDTO
					.setDiscountAllPrice(discountMessageDTO.getDiscountPrice() * discountMessageDTO.getBuyItemCount());
			discountMessageDTO.setOriginalPrice(mallItem.getPrice());
			discountMessageDTO
					.setOriginalAllPrice(discountMessageDTO.getOriginalPrice() * discountMessageDTO.getBuyItemCount());
			discountMessageDTOList.add(discountMessageDTO);
			discountDTO.setDiscountMessageDTOList(discountMessageDTOList);
			return;
		}

		for (MallMarketing mallMarketing : mallMarketingList) {
			discountMessageDTO = new DiscountMessageDTO();
			discountMessageDTO.setOriginalPrice(mallItem.getPrice());
			discountMessageDTO
					.setOriginalAllPrice(discountMessageDTO.getOriginalPrice() * discountMessageDTO.getBuyItemCount());
			discountMessageDTO.setBuyItemCount(discountDTO.getBuyItemCount());
			int type = mallMarketing.getType();
			if (type == 0) {/* 商品折扣 这个商品85折，如果是8折，存储为80 */
				discountMessageDTO.setDiscountPrice(new BigDecimal(mallItem.getPrice())
						.multiply(new BigDecimal(mallMarketing.getDiscount()).divide(new BigDecimal(100))).longValue());
				discountMessageDTO.setDiscountAllPrice(
						discountMessageDTO.getDiscountPrice() * discountMessageDTO.getBuyItemCount());
			} else if (type == 1 || type == 2) {/* 满减 代金券 满100减20 */
				Long originalallprice = discountMessageDTO.getOriginalAllPrice();
				if (originalallprice >= mallMarketing.getSatisfyCondition()) {
					discountMessageDTO.setDiscountAllPrice(
							discountMessageDTO.getOriginalAllPrice() - mallMarketing.getDiscount());
					discountMessageDTO.setDiscountPrice(null);
				}
			}
			discountMessageDTOList.add(discountMessageDTO);
		}

		return;
	}
}
